﻿using MVC_登陆登出及验证.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.ModelBinding;
using System.Web.Mvc;

namespace MVC_登陆登出及验证.Controllers
{
    public class AccountController : Controller
    {
        // GET: Account
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Login(string returnUrl)
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginViewModels mode, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                Session["UName"] = mode.UName;
            }
            var s = Request.Cookies;
            foreach (var item in s)
            {

            }
            return View();
        }
        public ActionResult Register()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel mode)
        {
            var str = "";
            if (!ModelState.IsValid)
            {
                foreach (var item in ModelState.Values)
                {
                    if (item.Errors.Count > 0)
                    {
                        foreach (var items in item.Errors)
                        {
                            str += items.ErrorMessage + "&&";//获取model的错误信息
                        }
                    }
                    var result = item.Value;
                    var tt = result.RawValue;//获取字段的值

                }
            }
         // var co=  Request.Cookies["cookusername"].Value;
            HttpCookie cook = new HttpCookie("cookusername","wabs");
            Response.Cookies.Add(cook);
            var rask = Task.Run<string>(() =>
            {// System.Threading.Thread.Sleep(10000);
                return "dasdddddddddddddddddddddddda";
            });
            //  Response.Write( rask.Result);
            //  RedirectToAction("Index","Home")
            //string encodeFileName = HttpUtility.UrlEncode("resa.js");
            //Response.AddHeader("content-Disposition", string.Format("attachment; filename ={0}", encodeFileName));//这句很重要,可以决定文件的保存位置
            //Response.ContentType = "appliaction/json";
    
            // ModelState.AddModelError("","醋无");
            return View();
        }

        public ActionResult Logout()
        {
            Session.Abandon();
            Session["UName"] = null;
            return Redirect("/Home/Index");
        }

    }

}